<template>
  <div>
    <h1>{{ id ? "编辑" : "新建" }}英雄</h1>
    <!-- <el-form label-width="120px" @submit.native.prevent="save"> -->
    <!-- <el-form label-width="120px" @submit.native.prevent :rules="rules"> -->
    <el-form label-width="120px" @submit.native.prevent>
      <el-tabs type="border-card" value="basic">
        <!-- 英雄的基础信息 -->
        <el-tab-pane label="基础信息" name="basic">
          <!-- <el-form-item label="名称" prop="name"> -->
          <el-form-item label="名称">
            <el-input v-model="model.name"></el-input>
          </el-form-item>

          <el-form-item label="称号">
            <el-input v-model="model.title"></el-input>
          </el-form-item>
          <!-- 英雄头像的上传 -->
          <el-form-item label="头像">
            <el-upload
              class="avatar-uploader"
              :action="mixinUploadURL"
              :headers="mixingetAuthHeaders()"
              :show-file-list="false"
              :on-success="res => $set(model, 'avatar', res.url)"
            >
              <img v-if="model.avatar" :src="model.avatar" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <!-- 上传Banner -->
          <el-form-item label="Banner">
            <el-upload
              class="avatar-uploader"
              :action="mixinUploadURL"
              :headers="mixingetAuthHeaders()"
              :show-file-list="false"
              :on-success="res => $set(model, 'banner', res.url)"
            >
              <img v-if="model.banner" :src="model.banner" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

          <!-- 选择英雄分类 -->
          <el-form-item label="类型">
            <el-select v-model="model.categories" multiple>
              <el-option
                v-for="item of categories"
                :key="item._id"
                :value="item._id"
                :label="item.name"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="难度">
            <el-rate
              style="margin-top: 0.7rem"
              :max="9"
              show-score
              v-model="model.scores.defficult"
            ></el-rate>
          </el-form-item>

          <el-form-item label="技能">
            <el-rate
              style="margin-top: 0.7rem"
              :max="9"
              show-score
              v-model="model.scores.skills"
            ></el-rate>
          </el-form-item>

          <el-form-item label="攻击">
            <el-rate
              style="margin-top: 0.7rem"
              :max="9"
              show-score
              v-model="model.scores.attack"
            ></el-rate>
          </el-form-item>

          <el-form-item label="生存">
            <el-rate
              style="margin-top: 0.7rem"
              :max="9"
              show-score
              v-model="model.scores.survive"
            ></el-rate>
          </el-form-item>

          <el-form-item label="顺风出装">
            <el-select v-model="model.items1" multiple>
              <el-option
                v-for="item of items"
                :key="item._id"
                :value="item._id"
                :label="item.name"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="逆风出装">
            <el-select v-model="model.items2" multiple>
              <el-option
                v-for="item of items"
                :key="item._id"
                :value="item._id"
                :label="item.name"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="使用技巧">
            <el-input type="textarea" v-model="model.usageTips"></el-input>
          </el-form-item>

          <el-form-item label="对抗技巧">
            <el-input type="textarea" v-model="model.battleTips"></el-input>
          </el-form-item>

          <el-form-item label="团战思路">
            <el-input type="textarea" v-model="model.teamTips"></el-input>
          </el-form-item>
        </el-tab-pane>
        <!-- 添加英雄技能 -->
        <el-tab-pane label="技能" name="skills">
          <el-button size="small" @click="model.skills.push({})"
            ><i class="el-icon-plus"></i>添加技能</el-button
          >
          <el-row type="flex" style="flex-wrap: wrap">
            <el-col :md="12" v-for="(item, index) in model.skills" :key="index">
              <el-form-item label="名称">
                <el-input v-model="item.name"></el-input>
              </el-form-item>
              <el-form-item label="图标">
                <!-- icon属性原先没定义 要用显示 赋值 -->
                <el-upload
                  class="avatar-uploader"
                  :action="mixinUploadURL"
                  :headers="mixingetAuthHeaders()"
                  :show-file-list="false"
                  :on-success="res => $set(item, 'icon', res.url)"
                >
                  <img v-if="item.icon" :src="item.icon" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item label="冷却值">
                <el-input type="input" v-model="item.delay"></el-input>
              </el-form-item>
              <el-form-item label="消耗">
                <el-input type="input" v-model="item.cost"></el-input>
              </el-form-item>
              <el-form-item label="描述">
                <el-input type="textarea" v-model="item.description"></el-input>
              </el-form-item>
              <el-form-item label="小提示">
                <el-input type="textarea" v-model="item.tips"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button
                  size="small"
                  type="danger"
                  @click="model.skills.splice(index, 1)"
                  >删除</el-button
                >
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
        <!-- 添加英雄拍档 -->
        <el-tab-pane label="最佳拍档" name="partners">
          <el-button size="small" @click="model.partners.push({})"
            ><i class="el-icon-plus"></i>添加英雄</el-button
          >
          <el-row type="flex" style="flex-wrap: wrap">
            <el-col
              :md="24"
              v-for="(item, index) in model.partners"
              :key="index"
            >
              <el-form-item label="英雄">
                <el-select filterable v-model="item.hero">
                  <el-option
                    v-for="hero in heroesList"
                    :key="hero._id"
                    :label="hero.name"
                    :value="hero._id"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="描述">
                <el-input type="textarea" v-model="item.description"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button
                  size="small"
                  type="danger"
                  @click="model.partners.splice(index, 1)"
                  >删除</el-button
                >
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>

      <el-form-item style="margin-top: 1rem">
        <el-button type="primary" navite-type="submit" @click="save"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    id: {}
  },
  data() {
    return {
      model: {
        name: "",
        avatar: "",
        scores: {
          defficult: 0
        },
        skills: [],
        partners: []
      },
      // 英雄分类数组
      categories: [],
      // 装备分类数组
      items: [],
      heroesList: []
    };
  },
  methods: {
    // 图片上传成功后执行的方法
    // afterUpload(res) {
    // vue 响应式绑定数据 未定义属性的对象 后面新增属性是不具备响应式的 要用 $set 来设置
    // this.$set(this.model, "avatar", res.url);
    // this.model.avatar = res.url;
    // console.log(this.model);
    // },
    // 新建分类的方法
    async save() {
      if (this.id) {
        // 修改物品名
        await this.$http.put(`rest/heroes/${this.id}`, this.model);
      } else {
        // console.log("save操作");
        // 提交数据
        await this.$http.post("rest/heroes", this.model);
        // console.log(res);
      }
      this.$router.push("/heroes/list");
      this.$message({
        type: "success",
        message: "保存成功"
      });
    },
    // 根据传过来的 _id 获取需要修改的物品名
    async fetch() {
      const res = await this.$http.get(`rest/heroes/${this.id}`);
      this.model = { ...this.model, ...res.data };
    },
    // 获取英雄分类的方法
    async fetchCategories() {
      const res = await this.$http.get(`rest/categories`);
      this.categories = res.data;
    },
    // 获取装备的方法
    async fetchItems() {
      const res = await this.$http.get("rest/items");
      this.items = res.data;
    },
    async fetchHeroesList() {
      const res = await this.$http.get("rest/heroes");
      this.heroesList = res.data;
    }
  },
  created() {
    this.fetchItems();
    this.fetchCategories();
    this.fetchHeroesList();
    this.id && this.fetch();
    // console.log(this.model.icon);
  }
};
</script>
